package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ISocDispatchEmpItemDAO;
import org.jsola.hr.dto.SocDispatchEmpItemAddDTO;
import org.jsola.hr.dto.SocDispatchEmpItemUpdateDTO;
import org.jsola.hr.entity.SocDispatchEmpItemDO;
import org.jsola.hr.query.SocDispatchEmpItemQuery;
import org.jsola.hr.service.ISocDispatchEmpItemService;
import org.jsola.hr.vo.SocDispatchEmpItemListVO;
import org.jsola.hr.vo.SocDispatchEmpItemVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 社保派单员工表_增员子项
 *
 * @author zhr
 */
@Service("hrSocDispatchEmpItemServiceImpl")
public class SocDispatchEmpItemServiceImpl implements ISocDispatchEmpItemService {

    @Autowired
    private ISocDispatchEmpItemDAO socDispatchEmpItemDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public SocDispatchEmpItemVO save(SocDispatchEmpItemAddDTO socDispatchEmpItemAddDTO, TokenUser tokenUser) {
        // 转DO
        SocDispatchEmpItemDO socDispatchEmpItemDO = socDispatchEmpItemAddDTO.to(SocDispatchEmpItemDO.class);
        // 保存
        socDispatchEmpItemDO = save(socDispatchEmpItemDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return socDispatchEmpItemDO.to(SocDispatchEmpItemVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(SocDispatchEmpItemUpdateDTO socDispatchEmpItemUpdateDTO, TokenUser tokenUser) {
        //转DO
        SocDispatchEmpItemDO socDispatchEmpItemDO = socDispatchEmpItemUpdateDTO.to(SocDispatchEmpItemDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(socDispatchEmpItemDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... socDispatchEmpItemIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) socDispatchEmpItemIds);
    }


    @Override
    public SocDispatchEmpItemVO selectById(String socDispatchEmpItemId, String siteId) {
        SocDispatchEmpItemDO socDispatchEmpItemDO = selectDOById(socDispatchEmpItemId, siteId);
        if (socDispatchEmpItemDO == null) {
            return null;
        }
        return socDispatchEmpItemDO.to(SocDispatchEmpItemVO.class);
    }

    @Override
    public List<SocDispatchEmpItemListVO> select(SocDispatchEmpItemQuery socDispatchEmpItemQuery, String siteId) {
        return getVoListByDoList(selectDO(socDispatchEmpItemQuery, siteId));
    }

    /**
     * 根据doList获取voList
     *
     * @param doList doList
     * @return voList
     */
    private List<SocDispatchEmpItemListVO> getVoListByDoList(List<SocDispatchEmpItemDO> doList) {
        if (CollectionUtils.isEmpty(doList)) {
            return doList == null ? null : new ArrayList<>();
        }
        return doList.stream()
                .map(socDispatchEmpItemDO -> socDispatchEmpItemDO.to(SocDispatchEmpItemListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(SocDispatchEmpItemQuery socDispatchEmpItemQuery, String siteId) {
        Example example = buildExample(socDispatchEmpItemQuery,siteId);
        return socDispatchEmpItemDAO.selectCountByExample(example);
    }

    @Override
    public Page<SocDispatchEmpItemListVO> selectPage(SocDispatchEmpItemQuery socDispatchEmpItemQuery, String siteId) {
        Example example = buildExample(socDispatchEmpItemQuery,siteId);
        Page<SocDispatchEmpItemDO> page = socDispatchEmpItemDAO.selectPageByExample(example,
                socDispatchEmpItemQuery.getPageNo(),
                socDispatchEmpItemQuery.getPageSize());

        return page.to(SocDispatchEmpItemListVO.class);
    }

    @Override
    public SocDispatchEmpItemDO selectDOById(String socDispatchEmpItemId, String siteId) {
        return listById(socDispatchEmpItemId, siteId);
    }

    @Override
    public List<SocDispatchEmpItemDO> selectDO(SocDispatchEmpItemQuery socDispatchEmpItemQuery, String siteId) {
        Example example = buildExample(socDispatchEmpItemQuery,siteId);
        return socDispatchEmpItemDAO.selectByExample(example);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public void batchSaveDoList(List<SocDispatchEmpItemDO> itemAddList, TokenUser tokenUser) {
        if (CollectionUtils.isEmpty(itemAddList)) {
            return;
        }
        itemAddList.parallelStream().forEach(
                item -> {
                    item.preInsert(tokenUser.getUserId());
                    item.setSiteId(tokenUser.getSiteId());
                }
        );
        socDispatchEmpItemDAO.insertListAndSetId(itemAddList);
    }

    @Override
    public List<SocDispatchEmpItemDO> listByEmpDispatchSocId(String empDispatchSocId, String siteId) {
        Example example = new Example(SocDispatchEmpItemDO.class);
        example.and()
                .andEqualTo("empDispatchSocId", empDispatchSocId)
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        example.orderBy("itemType").orderBy("itemName").orderBy("sort");
        return socDispatchEmpItemDAO.selectByExample(example);
    }

    @Override
    public List<SocDispatchEmpItemListVO> listVoByEmpDispatchSocId(String empDispatchSocId, String siteId) {
        return getVoListByDoList(listByEmpDispatchSocId(empDispatchSocId, siteId));
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public void deleteBySocDispatchEmpAddId(String empDispatchSocId, TokenUser tokenUser) {
        Example example = new Example(SocDispatchEmpItemDO.class);
        example.and()
                .andEqualTo("empDispatchSocId", empDispatchSocId)
                .andEqualTo("valid", true)
                .andEqualTo("siteId", tokenUser.getSiteId());
        socDispatchEmpItemDAO.deleteByExample(example);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public void deleteBySocDispatchEmpAddIds(String siteId, String... empDispatchSocIds) {
        if (empDispatchSocIds == null || empDispatchSocIds.length == 0) {
            return;
        }
        Example example = new Example(SocDispatchEmpItemDO.class);
        example.and()
                .andIn("empDispatchSocId", Arrays.asList(empDispatchSocIds))
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        socDispatchEmpItemDAO.deleteByExample(example);
    }

    @Override
    public List<SocDispatchEmpItemDO> listByEmpDispatchSocIds(String siteId, String... empDispatchSocIds) {
        if (empDispatchSocIds == null || empDispatchSocIds.length == 0) {
            return null;
        }
        Example example = new Example(SocDispatchEmpItemDO.class);
        example.and()
                .andIn("empDispatchSocId", Arrays.asList(empDispatchSocIds))
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        return socDispatchEmpItemDAO.selectByExample(example);
    }

    @Override
    public List<String> listItemNameBySocDispatch(String socDispatchId, String siteId) {
        return socDispatchEmpItemDAO.listItemNameBySocDispatch(socDispatchId, siteId);
    }


    /**
     * 根据查询参数，构建example

     * @param socDispatchEmpItemQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(SocDispatchEmpItemQuery socDispatchEmpItemQuery, String siteId) {
        Example example = new Example(SocDispatchEmpItemDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (socDispatchEmpItemQuery.getSocDispatchId() != null) {
            example.and().andEqualTo("socDispatchId", socDispatchEmpItemQuery.getSocDispatchId());
        }
        if (socDispatchEmpItemQuery.getEmpDispatchSocId() != null) {
            example.and().andEqualTo("empDispatchSocId", socDispatchEmpItemQuery.getEmpDispatchSocId());
        }
        if (socDispatchEmpItemQuery.getSocProgId() != null) {
            example.and().andEqualTo("socProgId", socDispatchEmpItemQuery.getSocProgId());
        }
        if (socDispatchEmpItemQuery.getItemType() != null) {
            example.and().andEqualTo("itemType", socDispatchEmpItemQuery.getItemType());
        }
        if (socDispatchEmpItemQuery.getItemName() != null) {
            example.and().andEqualTo("itemName", socDispatchEmpItemQuery.getItemName());
        }
        if (socDispatchEmpItemQuery.getCardinalityPerson() != null) {
            example.and().andEqualTo("cardinalityPerson", socDispatchEmpItemQuery.getCardinalityPerson());
        }
        if (socDispatchEmpItemQuery.getCardinalityDefaultStart() != null &&
                socDispatchEmpItemQuery.getCardinalityDefaultEnd() != null) {
            example.and().andBetween("cardinalityDefault", socDispatchEmpItemQuery.getCardinalityDefaultStart(),
                    socDispatchEmpItemQuery.getCardinalityDefaultEnd());
        }
        if (socDispatchEmpItemQuery.getCardinalityMinStart() != null &&
                socDispatchEmpItemQuery.getCardinalityMinEnd() != null) {
            example.and().andBetween("cardinalityMin", socDispatchEmpItemQuery.getCardinalityMinStart(),
                    socDispatchEmpItemQuery.getCardinalityMinEnd());
        }
        if (socDispatchEmpItemQuery.getCardinalityMaxStart() != null &&
                socDispatchEmpItemQuery.getCardinalityMaxEnd() != null) {
            example.and().andBetween("cardinalityMax", socDispatchEmpItemQuery.getCardinalityMaxStart(),
                    socDispatchEmpItemQuery.getCardinalityMaxEnd());
        }
        if (socDispatchEmpItemQuery.getPersentCompanyStart() != null &&
                socDispatchEmpItemQuery.getPersentCompanyEnd() != null) {
            example.and().andBetween("persentCompany", socDispatchEmpItemQuery.getPersentCompanyStart(),
                    socDispatchEmpItemQuery.getPersentCompanyEnd());
        }
        if (socDispatchEmpItemQuery.getPersentPersonalStart() != null &&
                socDispatchEmpItemQuery.getPersentPersonalEnd() != null) {
            example.and().andBetween("persentPersonal", socDispatchEmpItemQuery.getPersentPersonalStart(),
                    socDispatchEmpItemQuery.getPersentPersonalEnd());
        }
        if (socDispatchEmpItemQuery.getAmountCompanyStart() != null &&
                socDispatchEmpItemQuery.getAmountCompanyEnd() != null) {
            example.and().andBetween("amountCompany", socDispatchEmpItemQuery.getAmountCompanyStart(),
                    socDispatchEmpItemQuery.getAmountCompanyEnd());
        }
        if (socDispatchEmpItemQuery.getAmountPersonalStart() != null &&
                socDispatchEmpItemQuery.getAmountPersonalEnd() != null) {
            example.and().andBetween("amountPersonal", socDispatchEmpItemQuery.getAmountPersonalStart(),
                    socDispatchEmpItemQuery.getAmountPersonalEnd());
        }
        if (socDispatchEmpItemQuery.getAmountAll() != null) {
            example.and().andEqualTo("amountAll", socDispatchEmpItemQuery.getAmountAll());
        }
        if (socDispatchEmpItemQuery.getMantissaProcCompany() != null) {
            example.and().andEqualTo("mantissaProcCompany", socDispatchEmpItemQuery.getMantissaProcCompany());
        }
        if (socDispatchEmpItemQuery.getMantissaProcPersonal() != null) {
            example.and().andEqualTo("mantissaProcPersonal", socDispatchEmpItemQuery.getMantissaProcPersonal());
        }
        if (socDispatchEmpItemQuery.getItemRemark() != null) {
            example.and().andEqualTo("itemRemark", socDispatchEmpItemQuery.getItemRemark());
        }
        if (socDispatchEmpItemQuery.getNoError() != null) {
            example.and().andEqualTo("noError", socDispatchEmpItemQuery.getNoError());
        }
        if (socDispatchEmpItemQuery.getErrorMsg() != null) {
            example.and().andEqualTo("errorMsg", socDispatchEmpItemQuery.getErrorMsg());
        }
        if (socDispatchEmpItemQuery.getSort() != null) {
            example.and().andEqualTo("sort", socDispatchEmpItemQuery.getSort());
        }
        // 排序
        ExampleKit.setExampleOrder(example,socDispatchEmpItemQuery.getOrders());
        return example;
    }
}




